﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using Hosting.Class;
using System.Text.RegularExpressions;


namespace Hosting.Class
{
    public class Users : ProcessData
    {
        public String userName = null;
        public String passWord = null;
        public String phone = null;
        public String address = null;
        public String email = null;
        public bool active = true;
        private HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();

        public Users(String userName, String passWord, String phone, String address, String email, bool active) 
        {
            this.userName = userName;
            this.passWord = passWord;
            this.phone = phone;
            this.address = address;
            this.email = email;
            this.active = active;
        }

        public Users() { }

        public int isLogin(string username, string password)
        {
            try 
            {
                ConnectionDB conn = new ConnectionDB();
                if(conn.isConnection())
                {
                    HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                    //Retrieve users value by query
                    //IEnumerable<User> users = from u in db.Users 
                    //                          select u;

                    //Retrieve users password by sproc 
                    var users = db.PC_Users_Login(username);

                    foreach (var u in users)
                    {
                        if (username.Equals(u.UserName))
                        {
                            using (MD5 md5Hash = MD5.Create())
                            {
                                SecurityHash secure = new SecurityHash();
                                if (secure.VerifyMd5Hash(md5Hash, password, u.PassWord))
                                {
                                    if (u.Active == true)
                                    {
                                        return 1;
                                    }
                                    else
                                    {
                                        return 2;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                return -99;
            }


            return 0;
        }

        public int Add(string username, string password, string phone, string email, string address)
        {
            //try
            //{
                string hash = password;
                using (MD5 md5Hash = MD5.Create())
                {
                    SecurityHash secure = new SecurityHash();
                    hash = secure.GetMd5Hash(md5Hash, password);
                }
                int isInsert = db.PC_Users_Ins(username, hash, email, phone, address, true);
                return isInsert;
            //} catch(Exception e)
            //{
            //    db.Dispose();
            //    return -99;
            //}
            return -99;
            throw new NotImplementedException();
        }

        public int Edit()
        {
            throw new NotImplementedException();
        }

        public int Delete()
        {
            throw new NotImplementedException();
        }

        public void GetData()
        {
            throw new NotImplementedException();
        }

        public bool isActive()
        {
            throw new NotImplementedException();
        }

        /*
         * 1: Insert
         * 2: Edit
         * 3: Delete
         */
        public bool isCheck(string username, string password, string repassword, string phone, string email, string address, int kindCheck)
        {
            switch (kindCheck)
            {
                case 1:
                    int isValid = addValid(username, password, repassword, phone, email, address);
                    if(isValid == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case 2:
                    break;
                case 3:
                    break;
                default:
                    break;
            }

            return false;
        }

        /* addValid() method return:
         * 1: Validation is successful!
         * 10: Username input is null or not have value
         * 11: Username is less than 5 character
         * 12: Username is bigger than 50 character
         * 13: Username is have an special character, Valid: [a-zA-Z0-9]
         * 14: Username is existed
         * 20: Password input is null or not have value
         * 21: Password is less than 5 character
         * 22: Password is bigger than 100 character
         * 23: Password is not valid, Valid: [a-zA-Z0-9@!#$\'%^&*`~]
         * 24: Password and repassword is not match
         * 30: Email is not valid
         * 31: Email is too big
         * 40: Phone is null or not have value
         * 41: Phone must be number
         * 42: Phone is less than 10 character
         * 43: Phone is bigger than 15 character
         * 50: Address is null or not have value
         * 51: Address is too long
         */

        public bool isUsernameExist(string username)
        {
            var check = db.PC_UsersShow_GetOne(username).ToList();

            if (check.Count > 0)
            {
                return true;
            }

            return false;
        }

        public int addValid(string username, string password, string repassword, string phone, string email, string address)
        {
            /*USERNAME*/
            Regex rUsernameSpec = new Regex(@"[a-zA-Z0-9]{5,50}");
            Match mUsernameSpec = rUsernameSpec.Match(username);

            if (String.IsNullOrEmpty(username))
            {
                return 10;
            }
            else if(username.Length < 5)
            {
                return 11;
            }
            else if (username.Length > 50)
            {
                return 12;
            }
            else if (!mUsernameSpec.Success)
            {
                return 13;
            }
            else if (isUsernameExist(username))
            {
                return 14;
            }

            /*PASSWORD*/
            Regex rPasswordValid = new Regex(@"[a-zA-Z0-9@!#$\'%^&*`~]{0,100}");
            Match mPasswordValid = rPasswordValid.Match(password);

            if (String.IsNullOrEmpty(password))
            {
                return 20;
            }            
            else if (password.Length < 8)
            {
                return 21;
            }
            else if (password.Length > 50)
            {
                return 22;
            }
            else if (!mPasswordValid.Success)
            {
                return 23;
            }
            else if(!password.Equals(repassword))
            {
                return 24;
            }

            /*EMAIL*/
            Regex rEmailValid = new Regex(@"^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+${0,100}");
            Match mEmailValid = rEmailValid.Match(email);


            if (String.IsNullOrEmpty(email))
            {
                return 30;
            }
            else if (!mEmailValid.Success)
            {
                return 31;
            }

            /*Phone*/
            Regex rPhoneValid = new Regex(@"[0-9]*");
            Match mPhoneValid = rPhoneValid.Match(phone);

            if (String.IsNullOrEmpty(phone))
            {
                return 40;
            }
            else if (!mPhoneValid.Success)
            {
                return 41;
            }
            else if (phone.Length < 10)
            {
                return 42;
            }
            else if (phone.Length > 15)
            {
                return 43;
            }

            /*Address*/
            if (String.IsNullOrEmpty(address))
            {
                return 50;
            }
            else if (address.Length > 2000)
            {
                return 51;
            }

            return 1;
        }


        public int Add()
        {
            throw new NotImplementedException();
        }

        public bool isCheck()
        {
            throw new NotImplementedException();
        }
    }
}

/*Email varchar(100),
	Phone varchar(15),
	Address nvarchar(2000),*/